package cn.fql.ymcc.web.controller;

import cn.fql.ymcc.domain.Login;
import cn.fql.ymcc.feignclient.ILoginFeignClient;
import cn.fql.ymcc.service.ILoginLogService;
import cn.fql.ymcc.service.IUserService;
import cn.fql.ymcc.domain.User;
import cn.fql.ymcc.query.UserQuery;
import cn.fql.ymcc.result.JSONResult;
import cn.fql.ymcc.result.PageList;
import cn.fql.ymcc.util.RegisterPhone;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @Autowired
    private ILoginFeignClient iLoginFeignClient;
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JSONResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }
    /**
     * 用户手机号码注册
     */
    @RequestMapping(value="/register",method= RequestMethod.POST)
    @ApiOperation(value = "用户手机号码注册")
    public JSONResult registerByPhone(@RequestBody RegisterPhone registerPhone) throws ParseException, ParseException {
        Login login = new Login();
        login.setUsername(registerPhone.getPhone());
        login.setPassword(registerPhone.getPassword());
        login.setType(1);
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        iLoginFeignClient.saveOrUpdate(login);
        Login login1 = iLoginFeignClient.getOneByUsername(login.getUsername());
        User user = new User();
        user.setPhone(registerPhone.getPhone());
        user.setNickName(registerPhone.getPhone());
        user.setLoginId(login1.getId());
        //将现在的时间转换成Long
        Date d=new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time=sim.format(d);
        long s=0;
        s=sim.parse(time).getTime();
        user.setCreateTime(s);
        userService.insert(user);
        return JSONResult.success();
    }

}
